home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / BUILDCAT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-06  |  5.2 KB  |  193 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #ifndef _lint
  6. #include <unistd.h>
  7. #endif
  8.  
  9.  
  10. #if !defined(_lint)
  11. static char rcsid[] OPTIONAL = "$Id: buildcat.c,v 1.16 1997/09/07 00:31:16 root Exp root $";
  12. #endif
  13.  
  14. #define DATFILE        "catalog.dat"
  15. #define HDRFILE        "catalog.h"
  16. #define CATFILE        "/etc/catalog.cat"
  17. #define LABEL        "CATALOG_H"
  18.  
  19. #define NULLFILE    ((FILE *)0)
  20. #define NULLCHAR    ((char *)0)
  21. #define CTL        '@'
  22. #define DOSEOF        ('Z' - CTL)
  23.  
  24. int CATALOGPADDING = 10;
  25.  
  26.  
  27. int
  28. main (int argc, char *argv[])
  29. {
  30. int theindex = 1;
  31. int firstfile = 1;
  32. char buffer[256];
  33. char input[1024];
  34. char *cp, *cp2;
  35. FILE *in, *cat, *header;
  36. time_t now;
  37. int padding;
  38. long originalsize = 0;
  39. char *lastfilename = NULLCHAR;
  40.  
  41.  
  42.     /* check to see if an optional padding override is being given */
  43.     if (argc > 1)    {
  44.         CATALOGPADDING = atoi (argv[1]);
  45.         if (!CATALOGPADDING)
  46.             CATALOGPADDING = 1;
  47.     }
  48.     if (CATALOGPADDING != 1)
  49.         printf ("--- buildcat: Using padding offset of %d...\n", CATALOGPADDING);
  50.     
  51.     /* open the input data file */
  52.     if ((in = fopen (DATFILE, "rt")) == NULLFILE)    {
  53.         printf ("buildcat: can't open '%s'\n", DATFILE);
  54.         return (1);
  55.     }
  56.  
  57.     /* now, look for an existing file. If found, save it's size */
  58.     if ((cat  = fopen (NOSDIR CATFILE, "rb")) != NULLFILE)    {
  59.         fseek (cat, 0, SEEK_END);
  60.         originalsize = ftell (cat);
  61.         fclose (cat);
  62.         originalsize /= 256;
  63.     }
  64.  
  65.     /* open the output catalog file */
  66.     if ((cat = fopen (NOSDIR CATFILE, "wb")) == NULLFILE)    {
  67.         printf ("buildcat: can't open '%s'\n", NOSDIR CATFILE);
  68.         return (1);
  69.     }
  70.  
  71.     /* open the output header file */
  72.     if ((header = fopen (HDRFILE, "wt")) == NULLFILE)    {
  73.         printf ("buildcat: can't open '%s'\n", HDRFILE);
  74.         return (1);
  75.     }
  76.  
  77.     /* place the initial information in the header file */
  78.     (void) time (&now);
  79.     fprintf (header, "\n#ifndef %s\n#define %s\n\n/*\n"
  80.         " * Catalog header file for TNOS %s\n *\n * Built on %s *\n * Do NOT modify this file!\n *\n"
  81.         " * Instead, properly modify the '%s' file and execute 'buildcat'\n *\n */\n\n",
  82.         LABEL, LABEL, VERSION, ctime (&now), DATFILE);
  83.  
  84.     if (CATALOGPADDING != 1)
  85.         fprintf (header, "/* Padding each file to a multiple of %d */\n\n", CATALOGPADDING);
  86.  
  87.     /* and place an identification record as the first one in the cat file */
  88.     memset (buffer, 0, 256);
  89.     sprintf (buffer, "TNOS catalog file, based on TNOS %s\nCatalog built on %s"
  90.         "\n\n\n\n\n\n\n\n%c", VERSION, ctime (&now), DOSEOF);
  91.     fwrite (buffer, 1, 256, cat);
  92.  
  93.     /* loop while there is data left in the data file */
  94.     while (fgets (input, 1024, in))    {
  95.         /* if it is a comment, skip it */
  96.         if (*input == '\n' || *input == '#')
  97.             continue;
  98.  
  99.         /* if it is a string entry, but we haven't seen the first filename, then error */
  100.         if (*input != '[' && firstfile)    {
  101.             printf ("buildcat: parsing error. First filename not defined before "
  102.                 "first data line\n> %s\n", input);
  103.             return (1);
  104.         }
  105.  
  106.         /* is this a new filename ? */
  107.         if (*input == '[')    {    /* new filename */
  108.             if ((cp = strchr(&input[1], '.')) != NULLCHAR)
  109.                 *cp = 0;
  110.             /* add it to the header file */
  111.             padding = 0;
  112.             if (theindex != 1)    {
  113.                 while (theindex % CATALOGPADDING)    {
  114.                     memset (buffer, 0, 256);
  115.                     strcpy (buffer, "Undefined catalog string!\n");
  116.                     fwrite (buffer, 1, 256, cat);
  117.                     theindex++;
  118.                     padding++;
  119.                 }
  120.             }
  121.             if (padding)
  122.                 fprintf (header, "\t/* There are %d available strings in %s.c - filled with padding */\n",
  123.                     padding, lastfilename);
  124.             if (lastfilename)
  125.                 free (lastfilename);
  126.             lastfilename = strdup (&input[1]);
  127.             fprintf (header, "#define %s_catalog %d\n", &input[1], theindex);
  128.             firstfile = 0;
  129.             continue;
  130.         }
  131.  
  132.         /* make sure there is a string in quotation marks */
  133.         if ((cp = strchr (input, '\"')) == NULLCHAR || (cp2 = strrchr (++cp, '\"')) == NULLCHAR)    {
  134.             printf ("buildcat: parsing error. Line missing quotation marks!\n"
  135.                 "> %s\n", input);
  136.             return(1);
  137.         }
  138.         *cp2 = 0;
  139.  
  140.         /* check the length for < 256 bytes */
  141.         if (strlen (cp) > 255)    {
  142.             printf ("buildcat: parsing error. Line exceeds maximum length!\n"
  143.                 "> %s\n", input);
  144.             return (1);
  145.         }
  146.  
  147.         /* initialize the buffer to zeros, first, then copy the string */
  148.         memset (buffer, 0, 256);
  149.         cp2 = buffer;
  150.         while (*cp)    {
  151.             if (*cp == '\\')    {
  152.                 switch (cp[1])    {
  153.                     case 'n':    *cp2++ = '\n';
  154.                             cp += 2;
  155.                             break;
  156.                     case 'r':    *cp2++ = '\r';
  157.                             cp += 2;
  158.                             break;
  159.                     case 'b':    *cp2++ = '\b';
  160.                             cp += 2;
  161.                             break;
  162.                     case '\"':    *cp2++ = '\"';
  163.                             cp += 2;
  164.                             break;
  165.                     default:    *cp2++ = *cp++;
  166.                 }
  167.             } else
  168.                 *cp2++ = *cp++;
  169.         }
  170.  
  171.         /* now write it to the catalog file */
  172.         fwrite (buffer, 1, 256, cat);
  173.         theindex++;
  174.     }
  175.     /* write trailer to header file */
  176.     fprintf (header, "\n\nchar * catalog (int base, int theindex);"
  177.         "\nint catalog_check (void);"
  178.         "\n\n#endif /* %s */\n\n/* File complete */\n", LABEL);
  179.  
  180.     /* and close the files */
  181.     fclose (header);
  182.     fclose (cat);
  183.     fclose (in);
  184.  
  185.     /* tell the user all is well */
  186.     if (originalsize && originalsize != theindex)
  187.         printf ("--- buildcat: new catalog file size differs from previous one!\n");
  188.     printf ("--- buildcat: catalog file building complete!\n");
  189.     return 0;
  190. }
  191.  
  192.  
  193.